﻿using System;
using System.Collections;
using System.Text;
using System.Data;
using GenaralLibs;
using System.Net.Mail;
namespace Lists
{
    public class HoSoDKKD : DoiTuong
    {
        #region string ChuCoSo property
        public string ChuCoSo
        {
            get
            {
                if (_DataRow["ChuCoSo"] is string) return (string)_DataRow["ChuCoSo"];
                else return "";
            }
            set
            {
                _DataRow["ChuCoSo"] = value;
            }
        }
        #endregion

        #region string SoCMND property
        public string SoCMND
        {
            get
            {
                if (_DataRow["SoCMND"] is string) return (string)_DataRow["SoCMND"];
                else return "";
            }
            set
            {
                _DataRow["SoCMND"] = value;
            }
        }
        #endregion

        #region string TenCoSo property
        public string TenCoSo
        {
            get
            {
                if (_DataRow["TenCoSo"] is string) return (string)_DataRow["TenCoSo"];
                else return "";
            }
            set
            {
                _DataRow["TenCoSo"] = value;
            }
        }
        #endregion

        #region string SoGiayPhep property
        public string SoGiayPhep
        {
            get
            {
                if (_DataRow["SoGiayPhep"] is string) return (string)_DataRow["SoGiayPhep"];
                else return "";
            }
            set
            {
                _DataRow["SoGiayPhep"] = value;
            }
        }
        #endregion

        #region DateTime NgayCapPhep property
        public DateTime? NgayCapPhep
        {
            get
            {
                if (_DataRow["NgayCapPhep"] is DateTime) return (DateTime)_DataRow["NgayCapPhep"];
                return null;
            }
            set
            {
                _DataRow["NgayCapPhep"] = value;
            }
        }
        #endregion

        #region string CoQuanCap property
        public string CoQuanCap
        {
            get
            {
                if (_DataRow["CoQuanCap"] is string) return (string)_DataRow["CoQuanCap"];
                return "";
            }
            set
            {
                _DataRow["CoQuanCap"] = value;
            }
        }
        #endregion

        #region string DiaDiem_KD1 property
        public string DiaDiem_KD1
        {
            get
            {
                if (_DataRow["DiaDiem_KD1"] is string) return (string)_DataRow["DiaDiem_KD1"];
                return "";
            }
            set
            {
                _DataRow["DiaDiem_KD1"] = value;
            }
        }
        #endregion

        #region int DiaDiem_KD2 property
        public int DiaDiem_KD2
        {
            get
            {
                if (_DataRow["DiaDiem_KD2"] is int) return (int)_DataRow["DiaDiem_KD2"];
                return 0;
            }
            set
            {
                _DataRow["DiaDiem_KD2"] = value;
            }
        }
        #endregion

        public HoSoDKKD(DataRow dataRow)
            : base(dataRow)
        {
        }
    }

    public class DanhMucHoSoDKKD : DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucHoSoDKKD _Instance;
        public static DanhMucHoSoDKKD Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucHoSoDKKD();
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                }
                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "HoSoDKKD";
            }
        }
        #endregion

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new HoSoDKKD((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public HoSoDKKD this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new HoSoDKKD(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public HoSoDKKD NewDoiTuong()
        {
            try
            {
                return new HoSoDKKD(Instance.DataTable.NewRow());
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public HoSoDKKD GetByProperty(string property, object value)
        {
            try
            {
                DataRow[] rows = GetDataRows(property, value);
                if (rows.Length > 0) return new HoSoDKKD(rows[0]);
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public HoSoDKKD GetByID(int objID)
        {
            try
            {
                return GetByProperty("ID", objID);
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public HoSoDKKD GetBySoGiayPhep(string soGiayPhep)
        {
            try
            {
                return GetByProperty("SoGiayPhep", soGiayPhep);
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static DanhMucHoSoDKKD Filter(string filterExpression)
        {
            try
            {
                DanhMucHoSoDKKD obj = new DanhMucHoSoDKKD();
                obj.DataView = new DataView(DanhMucHoSoDKKD.Instance.DataTable, filterExpression, "", DataViewRowState.CurrentRows);
                return obj;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static DanhMucHoSoDKKD GetAll()
        {
            try
            {
                return Instance;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
    }
}